package cn.danger.com.db;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import java.util.ArrayList;
import java.util.List;

import cn.danger.com.model.entity.Operate;

/**
 * Created by 俊 on 2016/12/31.
 */
public class OperateDaoImpl implements OperateDao {

    private DBHelper helper;
    private static final String SQL_INSERT = "insert into operate(operate_id,username,operate_table," +
            "operate_objectId,operate_type) values(?,?,?,?,?)";
    private static final String SQL_UPDATE = "update operate set operate_objectId = ?,operate_type = ? " +
            "where operate_table = ? and operate_id = ?";
    private static final String SQL_DELETE = "delete from operate where operate_table = ? and operate_id = ?";
    private static final String SQL_QUERY = "select * from operate where operate_table = ? and operate_id = ?";
    private static final String SQL_QUERY_LIST = "select * from operate where username = ? and operate_table = ?";

    public OperateDaoImpl(Context context) {
        helper = DBHelper.getInstance(context);
    }

    @Override
    public void insert(Operate operate) {
        Operate origin_operate = queryOperate(operate.getOperate_table(), operate.getOperate_id());
        SQLiteDatabase db = helper.getWritableDatabase();
        if (origin_operate == null) {
            db.execSQL(SQL_INSERT, new Object[]{operate.getOperate_id(), operate.getUsername(), operate.getOperate_table(),
                    operate.getOperate_objectId(), operate.getOperate_type()});
            db.close();
        } else {
            db.close();
            update(origin_operate, operate);
        }
    }

    @Override
    public void update(Operate origin_operate, Operate operate) {
        SQLiteDatabase db = helper.getWritableDatabase();
        if (origin_operate.getOperate_type().equals("add") && operate.getOperate_type().equals("update")) {
            db.execSQL(SQL_UPDATE, new Object[]{operate.getOperate_objectId(), "add",
                    operate.getOperate_table(), operate.getOperate_id()});
            db.close();
        } else if (origin_operate.getOperate_type().equals("add") && operate.getOperate_type().equals("delete")) {
            db.close();
            delete(operate);
        } else if (origin_operate.getOperate_type().equals("update") && operate.getOperate_type().equals("delete")) {
            db.execSQL(SQL_UPDATE, new Object[]{operate.getOperate_objectId(), operate.getOperate_type(),
                    operate.getOperate_table(), operate.getOperate_id()});
            db.close();
        }
    }

    @Override
    public void delete(Operate operate) {
        SQLiteDatabase db = helper.getWritableDatabase();
        db.execSQL(SQL_DELETE, new Object[]{operate.getOperate_table(), operate.getOperate_id()});
        db.close();
    }

    @Override
    public Operate queryOperate(String operate_table, int operate_id) {
        SQLiteDatabase db = helper.getWritableDatabase();
        Operate operate = new Operate();
        Cursor cursor = db.rawQuery(SQL_QUERY, new String[]{operate_table, operate_id + ""});
        if (cursor.moveToNext()) {
            operate.setId(cursor.getInt(cursor.getColumnIndex("id")));
            operate.setOperate_id(cursor.getInt(cursor.getColumnIndex("operate_id")));
            operate.setOperate_type(cursor.getString(cursor.getColumnIndex("operate_type")));
            operate.setOperate_objectId(cursor.getString(cursor.getColumnIndex("operate_objectId")));
            operate.setOperate_table(cursor.getString(cursor.getColumnIndex("operate_table")));
        }
        cursor.close();
        db.close();
        if (operate.getOperate_table() == null) {
            return null;
        } else {
            return operate;
        }
    }

    @Override
    public List<Operate> queryOperates(String username, String operate_table) {
        SQLiteDatabase db = helper.getWritableDatabase();
        List<Operate> operates = new ArrayList<>();
        Cursor cursor = db.rawQuery(SQL_QUERY_LIST, new String[]{username, operate_table});
        while (cursor.moveToNext()) {
            Operate operate = new Operate();
            operate.setUsername(cursor.getString(cursor.getColumnIndex("username")));
            operate.setOperate_id(cursor.getInt(cursor.getColumnIndex("operate_id")));
            operate.setOperate_table(cursor.getString(cursor.getColumnIndex("operate_table")));
            operate.setOperate_objectId(cursor.getString(cursor.getColumnIndex("operate_objectId")));
            operate.setOperate_type(cursor.getString(cursor.getColumnIndex("operate_type")));
            operates.add(operate);
        }
        cursor.close();
        db.close();
        return operates;
    }
}
